<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
	
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<%@ taglib prefix="spring" uri="/WEB-INF/tlds/spring.tld"%>
<c:set var="BasePath" value="${pageContext.request.contextPath}"></c:set>
<c:set var="Plugins" value="${pageContext.request.contextPath}/res/plugins"></c:set>	

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>供应商货品价格设置页面</title>

<link href="${Plugins}/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/jquery/jquery-1.4.4.min.js"	type="text/javascript"></script>
<link href="${Plugins}/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/ligerCommon.js"></script>
<script type="text/javascript" src="${BasePath}/res/js/json2.js"></script>

<style type="text/css">
#searchbar{
   margin-bottom: 10px;
   margin-left:15px;
  }
  .navbar {
		height: 20px;
		border-bottom: #E6E6E6 solid 1px;
		line-height: 20px;
	}
.l-button-search {
	width: 50px;
	margin-left: 10px;
	padding-bottom: 2px;
	display: inline;
}
</style>
</head>
<body style="padding:6px; overflow:hidden;">
<div class="navbar">
		<span>您当前的位置：</span>
		商品管理 &gt; 
</div>

<form action="to_list_select_supplier.do" method="post" >
<input type="hidden" name="prodNo" id="prodNo" value="${prodNo }">
<input type="hidden" name="saleType" id="saleType" value="${saleType }">
<input type="hidden" name="salePrice" id="salePrice"  value="${salePrice}">
<input type="hidden" name="prodRate" id="prodRate"  value="${prodRate}">
<!-- 
<div id="searchbar"><br>
 供应商名称：<input id="supName" name="supName" type="text" value="${supplier.supName}" />
 供应商编号：<input id="account" name="account" type="text" value="${supplier.account}" />
      <input type="submit" id="btnSearch" value="查询" class="l-button l-button-search" />&nbsp;&nbsp;
      <input type="button" id="btnReset" value="重置" class="l-button l-button-search" />
</div> -->
</form>

<div id="maingrid" style="margin:0; padding:0"></div>

<script>	
var basePath='${BasePath}';
var grid = null;
var prodNo=$("#prodNo").val();
var list = ${list};
$(function () {
	   
		
 	// 重置
	$("#btnReset").click(function(){
   		$('#searchbar :input').not(':submit, :button, :reset').val('').removeAttr('checked').removeAttr('selected');
	});
 	

    var columns= [
			{ display: '供应商名称', name: 'supName'},
            { display: '供应商编号', name: 'account'},
             { display: '含税成本价',hide:true, render: function (rowdata, rowindex, value){
       			var val = "";
       	        if(list != null && list != ""){
       	        	for ( var i = 0; i < list.length; i++) {
       	        		if(list[i].prodNo != '${prodNo}') break;
       	        	   	if(list[i].supplierNo == rowdata.code){
       	        	   		val = list[i].taxCostPrice;
       	    			};
       				};
       	        };
               /*  var  text = "<input value='"+val+"' name='taxCostPrice' id='taxCostPrice" + rowdata.__id + "'   class='l-textarea' style='width: 50px;' onchange='javascript:validationIsNumber(this);calcGcrossMargin("+rowindex+");this.value = this.value.substring(0,9);'/>"; */
                var  text = "<input value='"+val+"' name='taxCostPrice' id='taxCostPrice" + rowdata.__id + "'   class='l-textarea' style='width: 50px;' />";
                return text;
            }},
                   
            { display: '未税成本价',hide:true, render: function (rowdata, rowindex, value){
            	var val = "";
                if(list != null && list != ""){
                	for ( var i = 0; i < list.length; i++) {
                  		if(list[i].prodNo != '${prodNo}') break;
                  	   	if(list[i].supplierNo == rowdata.code){
                  	   		val = list[i].untaxCostPrice;
              			};
          			};
				};
				/* var  text = "<input value='"+val+"'  name='untaxCostPrice' id='untaxCostPrice" + rowdata.__id + "'   class='l-textarea' style='width: 50px;' onchange='javascript:validationIsNumber(this);this.value = this.value.substring(0,9);'/>"; */
                var  text = "<input value='"+val+"'  name='untaxCostPrice' id='untaxCostPrice" + rowdata.__id + "'   class='l-textarea' style='width: 50px;' />";
                return text;
			}},
                      
			{ display: '毛利率',hide:true, render: function (rowdata, rowindex, value){
       			var val = "";
       	        if(list != null && list != ""){
       	        	for ( var i = 0; i < list.length; i++) {
       	           		if(list[i].prodNo != '${prodNo}') break;
       	        	   		if(list[i].supplierNo == rowdata.code){
       	        	   			val = list[i].grossMargin;
       	    				};
       				};
       	        };
               /*  var  text = "<input value='"+val+"' name='gcrossMargin' id='gcrossMargin" + rowdata.__id + "'  class='l-textarea' style='width: 50px;' onchange='javascript:validationIsNumber(this);this.value = this.value.substring(0,9);'/> %"; */
                var  text = "<input value='"+val+"' name='gcrossMargin' id='gcrossMargin" + rowdata.__id + "'  class='l-textarea' style='width: 50px;' /> %";
                return text;
			}}, 
       		 { display: '是否可退货',hide:true, render: function (rowdata, rowindex, value){
				var val = 1;
       	        if(list != null && list != ""){
       	        	for ( var i = 0; i < list.length; i++) {
       	        		if(list[i].prodNo != '${prodNo}') break;
       	        		if(list[i].supplierNo == rowdata.code){
       	        			val = list[i].isReturnedGoos;
       	    			};
       				};
       	        };
                var  text = "<select name='isReturnedGoods' id='isReturnedGoods" + rowdata.__id + "' class='l-textarea'>";
                text += "<option value='1' "+ (val==1 ? selected='selected' : '') +">是</opiton></select>";
                return text;
			}}, 
       		{ display: '供应商发货', render: function (rowdata, rowindex, value){
				var val = -1;
       	        if(list != null && list != ""){
       	        	for ( var i = 0; i < list.length; i++) {
       	        		if(list[i].prodNo != '${prodNo}') break;
       	        		if(list[i].supplierNo == rowdata.code){
       	        			val = list[i].isSupDelivery;
       	    			};
       				};
       	        };
                var  text = "<select name='isSupDelivery' id='isSupDelivery" + rowdata.__id + "' class='l-textarea'>";
                text += "<option value='' "+ (val==-1 ? selected='selected' : '') +" >请选择</opiton>";
                text += "<option value='0' "+ (val==0 ? selected='selected' : '') +" >否</opiton>";
                text += "<option value='1' "+ (val==1 ? selected='selected' : '') +">是</opiton></select>";
                return text;
			}}
	];

    var ischeckbox = false; 
    if('${saleType}'=='2'){
    	columns.unshift( { display: '选择', 
    		render: function (rowdata, rowindex, value){
    			var checked = "";
	        	if(list != null && list != ""){
	        		for ( var i = 0; i < list.length; i++) {
	        	   		if(list[i].prodNo != '${prodNo}') break;
	        	   		if(list[i].supplierNo == rowdata.code){
	        	   			checked = "checked=checked";
	    				};
					};
	        	};
    			var text="<input "+checked+" type='radio' name='radioSupplier' value='"+rowindex+"' />";
    			return text;
    		}
    	});
    }else{
    	ischeckbox = true;
    }
	grid = $("#maingrid").ligerGrid({
			checkbox: ischeckbox,
            columns: columns, 
            pageSize:30,
            width: '100%',
            height: '100%',
            dataType: 'server', 
            dataAction: 'server',
            pagesizeParmName: 'pageSize',
            parms: [
                    {name: "brandNo",value:"${brandNo}"},
                    {name: "saleType",value:"${saleType}"},
                    {name: "supName", value:"${supName}"},
                    {name: "account", value:"${account}"}
            	],
           url: "list_select_supplier.do",
           selectRowButtonOnly: true,
           isChecked: function(rowdata) {
        	   	var is = false;
        	   	if(list != null && list != ""){
        	   		for ( var i = 0; i < list.length; i++) {
        	   			if(list[i].prodNo != '${prodNo}') return false;
        	   			if(list[i].supplierNo == rowdata.code){
    						is = true;
    					}
					}
        	   	}
        	   	return is;
			},
           onBeforeCheckRow: function (checked, rowdata, rowindex) {
            	if (checked) {
            		/* if ($('#untaxCostPrice' + rowdata.__id).val() == '') {
            			$.ligerDialog.warn("请先输入未税成本价!");
            			return false;
            		}
            		if ($('#taxCostPrice' + rowdata.__id).val()=='') {
            			$.ligerDialog.warn("请先输入含税成本价!");
            			return false;
            		}
					if ($('#gcrossMargin' + rowdata.__id).val()=='') {
            			$.ligerDialog.warn("请先输入含税毛利率!");
            			return false;
            		} */
            		if ($('#isReturnedGoods' + rowdata.__id).val()=='') {
            			$.ligerDialog.warn("请先选择是否可退货!");
            			return false;
            		}
            		if ($('#isSupDelivery' + rowdata.__id).val()=='') {
            			$.ligerDialog.warn("请先选择是否供应商发货!");
            			return false;
            		}
            	}else {
            		//删除供应商价格设置
                	delect(rowdata);
            	}
			},
		   onBeforeCheckAllRow: function (checked, rowdata, rowindex) {
				for (var rowid in this.records) {
					if (checked) {
	            		/* if ($('#untaxCostPrice' + this.records[rowid].__id).val() == '') {
	            			$.ligerDialog.warn("请先输入未税成本价!");
	            			return false;
	            		}
	            		if ($('#taxCostPrice' + this.records[rowid].__id).val()=='') {
	            			$.ligerDialog.warn("请先输入含税成本价!");
	            			return false;
	            		}
						if ($('#gcrossMargin' + this.records[rowid].__id).val()=='') {
	            			$.ligerDialog.warn("请先输入含税毛利率!");
	            			return false;
	            		} */
	            		if ($('#isReturnedGoods' + this.records[rowid].__id).val()=='') {
	            			$.ligerDialog.warn("请先选择是否可退货!");
	            			return false;
	            		} 
	            		if ($('#isSupDelivery' + this.records[rowid].__id).val()=='') {
	            			$.ligerDialog.warn("请先选择是否供应商发货!");
	            			return false;
	            		}
					}else{
						delect(this.records[rowid]);
	            	}
	            }
			}
	});
});
 
 //验证输入价格的是否是数字
 function validationIsNumber(obj){
	 var i = $("input[name="+obj.name+"]").index($(obj));
	 var taxCostPrice = $("input[name=taxCostPrice]").eq(i).val();
	 var untaxCostPrice = $("input[name=untaxCostPrice]").eq(i).val();
	 if(parseFloat(taxCostPrice) < parseFloat(untaxCostPrice) && obj.name == 'untaxCostPrice'){
		 $.ligerDialog.warn("含税成本价不能大于未税成本价");
		 $("input[name=untaxCostPrice]").eq(i).val("");
	 }
	 if($("input[name=gcrossMargin]").eq(i).val()>100){
		 $.ligerDialog.warn("毛利率不能大于100%");
		 $("input[name=gcrossMargin]").eq(i).val("");
	 }
	 
	 var rule = /^[1-9]\d*|[1-9]\d*\.\d*|0\.\d*[1-9]\d*$/;
	 var value = obj.value;
	 if (value!='') {
		 value = parseFloat(value);
		 if(!rule.test(value)){
			 $.ligerDialog.warn("请输入数字!");
			 obj.value="";
		 }else{
			 obj.value=value;
		 };
	};
 }
 
 var salePrice = '${salePrice}'; //含税零售价
 var prodRate='${prodRate}';	//税率
 function calcGcrossMargin(rowindex){
	var gcrossMargin=0;
	var untaxCostPrice=0;
	
	var taxCostPrice = $("input[name=taxCostPrice]").eq(rowindex).val();
	if(salePrice!=""&&taxCostPrice!=""&&prodRate!=""){
		gcrossMargin = (Number(salePrice)-Number(taxCostPrice))/Number(salePrice)*100;
		untaxCostPrice=Number(taxCostPrice)/Number(1+Number(prodRate)/100);
		$("input[name=gcrossMargin]").eq(rowindex).val(gcrossMargin.toFixed(2));
		$("input[name=untaxCostPrice]").eq(rowindex).val(untaxCostPrice.toFixed(2));
	}
 }
 

//删除供应商价格设置
function delect(rowdata) {
	if(list != null && list != ""){
       	for ( var i = 0; i < list.length; i++) {
       		if(list[i].supplierNo == rowdata.code){
       			list.splice(i, 1);
   			};
		};
	};
}
	
	var isSuccess = null;
	//保存已选择的货品供应商设置信息
	function addProductToList() {
		var rows = [];
		if($("input[name=radioSupplier]:checked").val()){
			var row = {};
			row.__index = $("input[name=radioSupplier]:checked").val();
			rows.push(row);
		}else{
			rowdata = false;
			var manager = $("#maingrid").ligerGetGridManager();
			rows = manager.getSelectedRows();
		}
		var isradio = true;
		
		var supplierList = new Array();
		
		$.each(rows,function(o) {
			var i = rows[o].__index;
			var code = grid.getData()[i].code;
			var suppName = grid.getData()[i].suppName;
			/* var untaxCostPrice = $("input[name=untaxCostPrice]").eq(i).val();
			var taxCostPrice = $("input[name=taxCostPrice]").eq(i).val();
			var gcrossMargin = $("input[name=gcrossMargin]").eq(i).val(); */
			var isReturnedGoods = $("select[name=isReturnedGoods]").eq(i).val();
			var isSupDelivery = $("select[name=isSupDelivery]").eq(i).val();
			
			/* if (untaxCostPrice == '') {
				$.ligerDialog.warn("请先输入未税成本价!");
				isradio = false;
				return false;
			}
			if (taxCostPrice == '') {
				$.ligerDialog.warn("请先输入含税成本价!");
				isradio = false;
				return false;
			}
			if (gcrossMargin == '') {
				$.ligerDialog.warn("请先输入含税毛利率!");
				isradio = false;
				return false;
			} */
			if (isReturnedGoods == '') {
				$.ligerDialog.warn("请先选择是否可退货!");
				isradio = false;
				return false;
			}
			if (isSupDelivery == '') {
				$.ligerDialog.warn("请先选择是否供应商发货!");
				isradio = false;
				return false;
			}
			/* if(parseFloat(taxCostPrice) < parseFloat(untaxCostPrice)){
				$.ligerDialog.warn("含税成本价不能大于未税成本价");
				$("input[name=untaxCostPrice]").eq(i).val("");
				isradio = false;
				return false;
			}
			if($("input[name=gcrossMargin]").eq(i).val()>100){
				$.ligerDialog.warn("毛利率不能大于100%");
				$("input[name=gcrossMargin]").eq(i).val("");
				isradio = false;
				return false;
			} */
			 
			var obj = new Object();
			obj.prodNo = '${prodNo}';
			obj.code = code;
			obj.suppName = suppName;
			/* obj.untaxCostPrice = untaxCostPrice;
			obj.taxCostPrice = taxCostPrice;
			obj.gcrossMargin = gcrossMargin; */
			obj.isReturnedGoods = isReturnedGoods;
			obj.isSupDelivery = isSupDelivery;
			supplierList.push(obj);
		});
		/* $.each(list,function(index,item){
			if(JSON.stringify(supplierList).indexOf(item.supplierNo) == -1){
				var obj = new Object();
				obj.prodNo = item.prodNo;
				obj.code = item.supplierNo;
				obj.suppName = item.suppName;
				obj.untaxCostPrice = item.untaxCostPrice.toString();
				obj.taxCostPrice = item.taxCostPrice.toString();
				obj.gcrossMargin = item.grossMargin.toString();
				obj.isReturnedGoods = item.isReturnedGoos.toString();
				supplierList.push(obj);
			}
		});
		 */
		if (!isradio) {
			return false;
		}
		if (supplierList.length == 0) {
			$.ligerDialog.warn('请选择供应商');
		} else {
			var supplierArrayJson = JSON.stringify(supplierList);
			url = basePath + "/product/commoditymgt/add_set_prod_supplier.do";
			$.post(url, {
				'prodNo' : prodNo,
				'supplierList' : supplierArrayJson
			}, function(data) {
				isSuccess = data;
				if (data == "success") {
					var s = window.parent.$.ligerDialog.success("设置成功！", "提示",
							function() {
   								close_liger_tab();
							}); 
				} else {
					window.parent.$.ligerDialog.error("设置失败！");
				}
				;
			});
		}
	}
</script>

</body>
</html>